package com.mk.api.open.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.mk.api.open.domain.MtResult;
import com.mk.api.open.util.MktHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

/**
 * 〈接收美团外卖回调信息〉
 *
 * @author renqingqing
 * @create 2020/10/9
 */
@Slf4j
@RestController
@RequestMapping("/mt/waimai/v2")
public class NotifyV2Controller extends BaseController {


    @Value("${jd.post.url}")
    private String jdUrl;


    /**
     * https://developer.meituan.com/openapi#7.5.1
     * 接收订单数据接口
     * @return
     */
    @PostMapping("/order/create")
    public MtResult orderCreate(HttpServletRequest request) throws Exception{
        boolean verifyFlag = preHandle(request);
        if (!verifyFlag) {
            return MtResult.successV2();
        }
        Map<String, String> map = convertParams(request.getParameterMap());
        //String s = "[{\"actual_price\":28,\"app_food_code\":\"P_5O52SO3ES7Q6\",\"attr_counts\":\"2,4,2\",\"attr_names\":\"温度,糖度,茶底\",\"box_num\":1,\"box_price\":1,\"cart_id\":0,\"detail_extra\":{},\"food_discount\":1,\"food_name\":\"霸气草莓龙卷风\",\"food_property\":\"标准冰,少少甜,金牡丹\",\"mt_sku_id\":3783995135,\"mt_spu_id\":14944367821,\"mt_tag_id\":10822319042,\"original_price\":28,\"price\":28,\"quantity\":1,\"sku_id\":\"MTP_5O52SO3ES7Q6\",\"spec\":\"标准\",\"unit\":\"1人份\",\"weight\":-2},{\"actual_price\":23.8,\"app_food_code\":\"P_5HI6803PH05D\",\"attr_counts\":\"2,3,4,2\",\"attr_names\":\"配料,温度,糖度,茶底\",\"box_num\":1,\"box_price\":1,\"cart_id\":0,\"detail_extra\":{},\"food_discount\":1,\"food_name\":\"霸气一升桃\",\"food_property\":\"含茉莉茶冻,标准(冰沙),少少甜,鸭屎香茶\",\"mt_sku_id\":3587517754,\"mt_spu_id\":14444949279,\"mt_tag_id\":10822319042,\"original_price\":28,\"price\":28,\"quantity\":1,\"sku_id\":\"MTP_5HI6803PH05D\",\"spec\":\"标准\",\"unit\":\"1人份\",\"weight\":-2}]";
        //map.put("detail",s);
        String postJsonStr = JSON.toJSONString(map);
        log.info("美团外卖|接收订单推送消息V2,orderInfo={}", postJsonStr);
        // 推送信息到http接口
        String postResult = MktHttpUtil.postJson(jdUrl, postJsonStr);
        log.error("美团订单消息请求,jdUrl={},postResult={},postJsonStr={}",jdUrl,postResult,postJsonStr);
        return MtResult.successV2();
    }



    public Map<String, String> convertParams(Map<String, String[]> parameterMap) {
        try {
            log.info("====>####推送的参数【{}】", JSON.toJSONString(parameterMap));
            return parameterMap.entrySet().stream().collect(toMap(
                    Map.Entry::getKey,
                    m -> m.getValue()[0]
            ));
        } catch (Exception e) {
            log.error("====>####推送数据转换请求参数发生异常，异常信息如下", e);
        }
        return new HashMap<>();
    }
}
